/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cdp.cdf.component.metadata.serializer.factory;

import com.inspur.edp.caf.cef.schema.base.datatype.impl.DefaultCollectionType;
import com.inspur.edp.caf.cef.schema.base.datatype.impl.DefaultDtoType;
import com.inspur.edp.caf.cef.schema.base.datatype.impl.DefaultMapType;
import com.inspur.edp.caf.cef.schema.base.datatype.impl.DefaultPrimitiveType;
import com.inspur.edp.caf.cef.schema.base.datatype.serializer.DataTypeDeSerializer;
import com.inspur.edp.caf.cef.schema.base.datatype.serializer.DataTypeSerializer;
import com.inspur.edp.caf.cef.schema.base.datatype.serializer.impl.DefaultCollectionTypeSerializer;
import com.inspur.edp.caf.cef.schema.base.datatype.serializer.impl.DefaultDtoTypeSerializer;
import com.inspur.edp.caf.cef.schema.base.datatype.serializer.impl.DefaultMapTypeSerializer;
import com.inspur.edp.caf.cef.schema.base.datatype.serializer.impl.DefaultPrimitiveTypeSerializer;
import com.inspur.edp.caf.cef.schema.base.datatype.serializer.impl.ObjectTypeSerializer;
import com.inspur.edp.caf.cef.schema.base.serializer.DataTypeSerializerFactory;
import com.inspur.edp.caf.cef.schema.datatype.CollectionType;
import com.inspur.edp.caf.cef.schema.datatype.DataType;
import com.inspur.edp.caf.cef.schema.datatype.MapType;
import com.inspur.edp.caf.cef.schema.datatype.ObjectType;
import com.inspur.edp.caf.cef.schema.datatype.PrimitiveType;
import com.inspur.edp.cdf.component.api.exception.CommonComponentException;
import com.inspur.edp.cdp.cdf.component.manager.serializer.StructureTypeReferSerializer;
import com.inspur.edp.cdp.cdf.component.metadata.exception.MetadataExceptionCode;
import com.inspur.edp.component.schema.StructuredTypeRefer;

@Deprecated
public class CmpDataTypeSerializerFactory implements DataTypeSerializerFactory {

    @Override
    public DataTypeSerializer getSerializer(DataType dataType) {
        if (dataType instanceof PrimitiveType) {
            return new DefaultPrimitiveTypeSerializer();
        } else if (dataType instanceof CollectionType) {
            return new DefaultCollectionTypeSerializer();
        } else if (dataType instanceof MapType) {
            return new DefaultMapTypeSerializer();
        } else if (dataType instanceof DefaultDtoType) {
            return new DefaultDtoTypeSerializer();
        } else if (dataType instanceof StructuredTypeRefer) {
            return new StructureTypeReferSerializer();
        } else if (dataType instanceof ObjectType) {
            return new ObjectTypeSerializer();
        } else {
            throw new CommonComponentException(MetadataExceptionCode.SERVICE_UNIT_CODE, MetadataExceptionCode.RESOURCE_FILE,
                    MetadataExceptionCode.GSP_COMMONCMP_METADATA_0001, null, false, dataType.getKind());
        }
    }

    @Override
    public DataTypeDeSerializer getDeSerializer(String kind) {
        switch (kind) {
            case DefaultPrimitiveType.KIND:
                return new DefaultPrimitiveTypeSerializer();
            case DefaultCollectionType.KIND:
                return new DefaultCollectionTypeSerializer();
            case DefaultMapType.KIND:
                return new DefaultMapTypeSerializer();
            case DefaultDtoType.KIND:
                return new DefaultDtoTypeSerializer();
            case StructuredTypeRefer.KIND:
                return new StructureTypeReferSerializer();
            case ObjectType.KIND:
                return new ObjectTypeSerializer();
            default:
                throw new CommonComponentException(MetadataExceptionCode.SERVICE_UNIT_CODE, MetadataExceptionCode.RESOURCE_FILE,
                        MetadataExceptionCode.GSP_COMMONCMP_METADATA_0002, null, false, kind);
        }
    }
}
